﻿using System;
using System.Xml.Serialization;
using EpiNEXT.Services.Fields.Properties;

namespace EpiNEXT.Services.Fields
{
    public enum DateFormatMode
    {
        Default,        // use current culture settings
        MonthDayYear,
        DayMonthYear,
        YearMonthDay
    }

    public enum DateTimeType
    {
        Date,
        DateTime,
        Time
    }

    //[Serializable()]
    public class DateBoxField : DateField, IInputField
    {
        #region Members
        private string _formatModeString = "Default";
        private string _dateTimeTypeString = "Date";
        private DateFormatMode _formatMode = DateFormatMode.Default;
        private DateTimeType _dateTimeType = DateTimeType.Date;
        #endregion // Members

        #region Constructors
        public DateBoxField()
            : base()
        {
            Separator = "/";
            InputMask = "00" + Separator + "00" + Separator + "0000";
            DateTimeTypeString = "Date";
            FormatModeString = "Default";
            BuildMask();
        }
        #endregion // Constructors

        #region Properties
        /// <summary>
        /// The date separator to apply to the mask
        /// </summary>
        [XmlElement]
        public string Separator { get; set; }

        /// <summary>
        /// The date separator to apply to the mask
        /// </summary>
        [XmlElement("FormatMode")]
        public string FormatModeString
        {
            get
            {
                return this._formatModeString;
            }
            set
            {
                if (!FormatModeString.Equals(value, StringComparison.OrdinalIgnoreCase))
                {
                    this._formatModeString = value;

                    switch (FormatModeString)
                    {
                        case "Default":
                            FormatMode = DateFormatMode.Default;
                            break;
                        case "DayMonthYear":
                            FormatMode = DateFormatMode.DayMonthYear;
                            break;
                        case "MonthDayYear":
                            FormatMode = DateFormatMode.MonthDayYear;
                            break;
                        case "YearMonthDay":
                            FormatMode = DateFormatMode.YearMonthDay;
                            break;
                    }

                    RaisePropertyChanged("FormatMode");
                    RaisePropertyChanged("FormatModeString");
                    BuildMask();
                }
            }
        }

        [XmlIgnore]
        public DateFormatMode FormatMode
        {
            get
            {
                return this._formatMode;
            }
            set
            {
                if (FormatMode != value)
                {
                    this._formatMode = value;

                    switch (FormatMode)
                    {
                        case DateFormatMode.Default:
                            FormatModeString = "Default";
                            break;
                        case DateFormatMode.DayMonthYear:
                            FormatModeString = "DayMonthYear";
                            break;
                        case DateFormatMode.MonthDayYear:
                            FormatModeString = "MonthDayYear";
                            break;
                        case DateFormatMode.YearMonthDay:
                            FormatModeString = "YearMonthDay";
                            break;
                    }

                    RaisePropertyChanged("FormatMode");
                    RaisePropertyChanged("FormatModeString");
                    BuildMask();
                }
            }
        }

        /// <summary>
        /// The date input mask to apply to this field
        /// </summary>
        [XmlIgnore]
        public string InputMask { get; set; }

        [XmlElement("DateTimeType")]
        public string DateTimeTypeString
        {
            get
            {
                return this._dateTimeTypeString;
            }
            set
            {
                if (!DateTimeTypeString.Equals(value, StringComparison.OrdinalIgnoreCase))
                {
                    this._dateTimeTypeString = value;

                    switch (DateTimeTypeString)
                    {
                        case "Date":
                        default:
                            DateTimeType = Fields.DateTimeType.Date;
                            break;
                        case "DateTime":
                            DateTimeType = Fields.DateTimeType.DateTime;
                            break;
                        case "Time":
                            DateTimeType = Fields.DateTimeType.Time;
                            break;
                    }

                    RaisePropertyChanged("DateTimeType");
                    RaisePropertyChanged("DateTimeTypeString");
                    BuildMask();
                }
            }
        }

        [XmlIgnore]
        public DateTimeType DateTimeType
        {
            get
            {
                return this._dateTimeType;
            }
            set
            {
                if (DateTimeType != value)
                {
                    this._dateTimeType = value;

                    switch (DateTimeType)
                    {
                        case Fields.DateTimeType.Date:
                        default:
                            DateTimeTypeString = "Date";
                            break;
                        case Fields.DateTimeType.DateTime:
                            DateTimeTypeString = "DateTime";
                            break;
                        case Fields.DateTimeType.Time:
                            DateTimeTypeString = "Time";
                            break;
                    }

                    RaisePropertyChanged("DateTimeType");
                    RaisePropertyChanged("DateTimeTypeString");
                    BuildMask();
                }
            }
        }
        #endregion // Properties

        #region Methods
        private void BuildMask()
        {
            string mask = String.Empty;

            if (DateTimeType != Fields.DateTimeType.Time)
            {
                switch (FormatMode)
                {
                    case DateFormatMode.Default:
                    case DateFormatMode.DayMonthYear:
                    case DateFormatMode.MonthDayYear:
                        mask = "00" + Separator + "00" + Separator + "0000";
                        break;
                    case DateFormatMode.YearMonthDay:
                        mask = "0000" + Separator + "00" + Separator + "00";
                        break;
                }
            }

            if (DateTimeType != Fields.DateTimeType.Date)
            {
                mask = mask + " " + "00:00:00";
            }

            InputMask = mask;
        }
        #endregion // Methods
    }
}
